@inherits BaseSection

<div class="content">
    <table class="table">
        <thead>
            <tr>
                @{
                    if (CollapseEmptyCells && UI.CommonFields != null)
                    {
                        foreach (var field in UI.CommonFields)
                        {
                            <HeaderCell Field="field" OnPageChanged="@OnPageChanged" />
                        }

                        if (UI.MaxUniqueFieldsInSingleEntity - UI.CommonFields.Count > 0)
                        {
                            <td colspan=@(UI.MaxUniqueFieldsInSingleEntity - UI.CommonFields.Count)></td>
                        }
                    }
                    else if (UI.UniqueFields != null)
                    {
                        foreach (var field in UI.UniqueFields)
                        {
                            <HeaderCell Field="field" />
                        }
                    }

                    if (UI.SectionsHaveButtons)
                    {
                        <td class="button-cell"></td>
                    }
                }
            </tr>
        </thead>
        <tbody>
            @foreach (var (context, sections) in Sections)
            {
                <CascadingEditContext EditContext=@context>
                    @foreach (var section in sections)
                    {
                        <RowSection Section="section"
                                    UI="UI"
                                    OnButtonClick="OnButtonClick"
                                    DragInteraction="DragInteraction"/>
                    }
                </CascadingEditContext>
            }

            @if (!Sections.Any())
            {
                <tr>
                    <td colspan="99">&nbsp;</td>
                </tr>
            }
            else
            {
                <tr
                    @ondragenter="(args) => DragInteraction.EndZoneDragEnter(args, SectionId)"
                    class="@(DragInteraction.IsDraggedOverEndZone(SectionId) ? "dropover" : "")">
                    <td colspan="99">&nbsp;</td>
                </tr>
            }
        </tbody>
    </table>

    <Paginator CurrentPage="@CurrentPage"
               MaxPage="@MaxPage"
               OnPageChanged="@OnPageChanged" />
</div>

@code {
    private bool CollapseEmptyCells => UI.EmptyVariantColumnVisibility == EmptyVariantColumnVisibility.Collapse;

    [Parameter] public IEnumerable<(FormEditContext editContext, IEnumerable<SectionUI> sections)> Sections { get; set; } = default!;
    [Parameter] public ListUI UI { get; set; } = default!;
    [Parameter] public EventCallback<ButtonClickEventArgs> OnButtonClick { get; set; }
    [Parameter] public EventCallback<RowDragEventArgs> OnRowDragged { get; set; }

    [Parameter] public int CurrentPage { get; set; }
    [Parameter] public int? MaxPage { get; set; }
    [Parameter] public EventCallback<int> OnPageChanged { get; set; }
    
    [Inject] private IDragInteraction DragInteraction { get; set; } = default!;

    protected override void OnInitialized()
    {
        base.OnInitialized();

        DragInteraction.DragCompletion += OnDragCompletion;
        DragInteraction.DragStateChange += OnDragStateChange;
    }

    ~TableSection()
    {
        DragInteraction.DragCompletion -= OnDragCompletion;
        DragInteraction.DragStateChange -= OnDragStateChange;
    }

    private async void OnDragCompletion(object? sender, RowDragEventArgs args)
    {
        await OnRowDragged.InvokeAsync(args);
    }

    private void OnDragStateChange(object? sender, EventArgs args)
    {
        StateHasChanged();
    }
}
